<script src="../../../store/getters.js"></script>
<template>
  <div class="tenantMenu_main main_container">
    <div class="main_header">
      <div class="padding_container">
        <div class="left"><Breadcrumb /></div>
        <div class="right">
          <HeaderOperation :parent="this" />
        </div>
      </div>
    </div>
    <div v-loading="loading" class="main_main">
      <SearchBar :parent="this" />
      <div ref="table_container" class="table_container">
        <el-table
          v-if="tableColumns.length>0"
          ref="table"
          :data="tableData"
          stripe
          border
          :height="tableHeight"
          style="width: 99.9%"
          row-key="id"
          :cell-style="cellStyle"
          @cell-click="cellClick"
          @sort-change="(column)=>{sortChange(column, this)}"
          @selection-change="(val)=>{selectionChange(val, this)}"
        >
          <el-table-column type="selection" width="70" align="center" />
          <Column v-for="(column,index) in tableColumns" :key="index" :prop="column.prop" :is-show="column.isShow" :label="column.label" :sortable="column.sortable" :width="column.width" />
          <el-table-column label="操作" align="center" fixed="right" width="180">
            <template slot-scope="scope">
              <el-button
                v-if="scope.row.status =='0' || scope.row.status =='1' || scope.row.status =='4'"
                v-hasPermi="['system:notice:edit']"
                type="text"
                @click.stop="edit(scope.row)"
              >编辑</el-button>
              <button v-else class="el-button--text button_grey">编辑</button>
              <el-button
                v-if="scope.row.status =='1' || scope.row.status =='2'"
                type="text"
                @click.stop="cancel(scope.row)"
              >撤销</el-button>
              <span v-else class="el-button--text button_grey">撤销</span>
              <el-button
                v-if="scope.row.status !=='2'"
                v-hasPermi="['system:notice:delete']"
                type="text"
                @click.stop="deleteOne(scope.row)"
              >删除</el-button>
              <span v-else class="el-button--text button_grey">删除</span>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <div class="main_bottom">
      <pagination
        :total="total"
        :page.sync="pageData.pageNum"
        :limit.sync="pageData.pageSize"
        :selected-num="selectionsAll.length"
        @pagination="getList"
      />
    </div>

  </div>
</template>

<script>
import { list, remove, repealDeliver } from '@/api/system/notice'
import AddOrUpdate from './addOrUpdate' // 新增或编辑页面
import ReadUnread from './readUnread'
import ShowDetailOld from './showDetail'
import { mapGetters } from 'vuex'
export default {
  name: 'Index',
  computed: {
    ...mapGetters([
       'notificationChangeTag'
    ])
  },
  watch: {
    notificationChangeTag() {
      this.getList()
    }
  },
  data() {
    return {
      loading: true,
      pageId: '系统公告', // 页面唯一标识
      pageData: {
        pageNum: 1,
        pageSize: 20,
        orderByColumn: 'updateTime',
        isAsc: 'desc',
        userIdStr: []
      },
      typeData: {
        status: 1
      },
      readStr: [],
      readUserIds: '',
      notReadUserIds: '',
      total: 1, // 数据一共有多少条
      mainId: 'id', // 表格行ID
      tableHeight: '0', // 表格高度
      tableData: [], // 表格数据
      tableColumns: [], // 表格实际用的字段
      searchColumns: [], // 搜索区实际用的字段
      selections: [], // 表格当前页所选中的行
      selectionsAll: [], // 表格所有页选中的行
      defaultTableColumns: [// 表格常规字段（初始值）
        { prop: 'statusName', label: '公告状态', canOrder: false },
        { prop: 'noticeTitle', label: '公告主题', canOrder: false },
        { prop: 'userName', label: '发布人员', canOrder: false },
        { prop: 'noticeTime', label: '公告发布时间', sortable: 'custom' },
        { prop: 'endTime', label: '公告截止时间', sortable: 'custom' },
        { prop: 'readStr', label: '已读进度', canOrder: false },
        { prop: 'createTime', label: '创建时间', sortable: 'custom' }
      ],
      defaultSearchColumns: [// 搜索区常规字段（初始值）
        { prop: 'userName', label: '发布人员', type: 'input' },
        { prop: 'status', label: '公告状态', type: 'select', optionsName: 'statusOptions' },
        { prop: 'noticeTime', label: '公告发布时间', type: 'time' },
        { prop: 'endTime', label: '公告截止时间', type: 'time' }
      ],
      statusOptions: [ // 选择组件数据源
        { value: undefined, label: '全部' },
        { value: 1, label: '待发布' },
        { value: 0, label: '未发布' },
        { value: 2, label: '已发布' },
        { value: 3, label: '已到期' },
        { value: 4, label: '已撤销' }
      ],
      operationData: [ // 右上角的操作按钮
        { label: '新增', perm: 'system:notice:add' },
        // { label: '导入', perm: 'device:baseStandingBook:import' },
        { label: '批量操作', perm: '' },
        { label: '批量删除', perm: '' },
        // { label: '批量导出', perm: 'device:baseStandingBook:export' },
        // { label: '打印', perm: '' },
        { label: '页面配置', perm: '' }
      ],

      equipmentInfoOptions: []
    }
  },
  methods: {
    cellStyle({ row, column }) {
      if (column.property == 'noticeTitle') {
        return {
          cursor: 'pointer',
          color: '#387bfc'
        }
      }
      if (column.property == 'readStr') {
        return {
          cursor: 'pointer',
          color: '#387bfc'
        }
      }
    },
    cellClick(row, column, cell) {
      switch (column.property) {
        case 'noticeTitle':
          this.seeDetail(row)
          break
        case 'readStr':
          if (row.readUserIds) {
            this.readUserIds = row.readUserIds.toString()
          }
          if (row.notReadUserIds) {
            this.notReadUserIds = row.notReadUserIds.toString()
          }
          this.readUnread(row)
      }
    },
    getList() {
      list(this.pageData).then(res => {
        this.tableData = res.data
        // 判断能不能删除，存入标识
        this.tableData.forEach(item => {
          if (item.status == 2) {
            item.canNotSelectDelete = true // 不能删除的行给标记
          }
        })
        this.tableData.forEach(item => {
          if (item.createTime) {
            item.createTime = item.createTime.slice(0, -3)
          }
        })
        console.log(this.tableData)
        this.total = res.total
        this.$nextTick(() => {
          this.setSelectRow(this)// 设置行选中
          this.tableHeight = this.$refs.table_container.offsetHeight
        })
      })
    },
    getExportData(data) {
      return list(data)
    },
    add() {
      this.$layer.iframe({// 弹框
        content: {
          content: AddOrUpdate, // 传递的组件对象
          parent: this, // 当前的vue对象
          data: {
          }// props
        },
        area: ['1320px', '900px'],
        title: '发布企业公告', // 标题
        shadeClose: false // 点击遮罩是否关闭
      })
    },
    edit(row) {
      this.$layer.iframe({// 弹框
        content: {
          content: AddOrUpdate, // 传递的组件对象
          parent: this, // 当前的vue对象
          data: {
            id: row[this.mainId]
          }// props
        },
        area: ['1320px', '900px'],
        title: '编辑企业公告', // 标题
        shadeClose: false // 点击遮罩是否关闭
      })
    },
    readUnread(row) {
      this.$layer.iframe({// 弹框
        content: {
          content: ReadUnread, // 传递的组件对象
          parent: this, // 当前的vue对象
          data: {
            readUserIds: this.readUserIds,
            notReadUserIds: this.notReadUserIds,
            readStr: row.readStr
          }// props
        },
        area: ['800px', '520px'],
        title: '已读进度', // 标题
        shadeClose: false // 点击遮罩是否关闭
      })
    },
    seeDetail(row) {
      this.$layer.iframe({// 弹框
        content: {
          content: ShowDetailOld, // 传递的组件对象
          parent: this, // 当前的vue对象
          data: {
            id: row.id
          }// props
        },
        area: ['880px', '820px'],
        title: '公告详情', // 标题
        shadeClose: false // 点击遮罩是否关闭
      })
    },
    cancel(row) {
      const _this = this
      this.$confirm('是否确定撤销该公告？', '撤销确认', {
        confirmButtonText: '确定撤销',
        cancelButtonText: '取消', cancelButtonClass: 'cancelConfirm',
        type: 'warning'
      }).then(function() {
        row.createTime = ''
        row.noticeContent = ''
        repealDeliver(row).then(res => {
          _this.$message({
            message: res.msg,
            type: 'success'
          })
          _this.selectionsAll = []
          _this.getList()
        })
      }).catch(() => {})
    },
    closeLayer() {
      console.log(this.$layer)
    },
    deleteOne(row) {
      const _this = this
      this.$confirm('确定要删除吗', '删除确认', {
        confirmButtonText: '确定',
        cancelButtonText: '取消', cancelButtonClass: 'cancelConfirm',
        type: 'warning'
      }).then(function() {
        remove(row[_this.mainId]).then(res => {
          _this.$message({
            message: res.msg,
            type: 'success'
          })
          _this.getList()
        })
      }).catch(() => {})
    },
    // 当前页的删除接口
    deleteMethod(deleteIds) {
      return remove(deleteIds)
    }
  }
}
</script>

<style lang="less" scoped>
</style>
